package com.example.demo.framework.util;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

public class QiniuHelper {

	static final String accessKey = "74fIstkhY-FN2zI56bWxT1S5V1FqjHbc4jzahpUr";
	static final String secretKey = "A69HuN6cpCkqIPvg97lfeJ299vrWFQPRZhxj7I00";
	static final String bucket = "dr5858";
	public static final String domain = "http://7xo9kp.com1.z0.glb.clouddn.com";
	public static final String IMAGEVIEW2_5_W_100 = "?imageView2/5/w/100";

	public static final String UPLOADTYPE_ITEM = "item";

	public static Auth auth() {
		return Auth.create(accessKey, secretKey);
	}

	public static String uploadFile(byte[] byteData, String key) {
		Configuration cfg = new Configuration(Zone.zone0());
		UploadManager uploadManager = new UploadManager(cfg);
		String token = auth().uploadToken(bucket);
		Response response;
		String result = "";
		try {
			response = uploadManager.put(byteData, key, token, null, null, false);
			result = response.bodyString();
		} catch (QiniuException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
	public static void deleteFile(String key) {
		Configuration cfg = new Configuration(Zone.zone0());
		BucketManager bucketManager = new BucketManager(auth(), cfg);
		try {
			bucketManager.delete(bucket, key);
		} catch (QiniuException ex) {
			System.err.println(ex.code());
			System.err.println(ex.response.toString());
		}
	}

}
